package com.meisiabc.app.web.advice;

import com.meisiabc.app.web.auth.InvalidAuthException;
import com.meisiabc.support.Result;
import com.meisiabc.utils.VerifyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Created by sunchangpeng
 */
@Slf4j
@ControllerAdvice
public class ResponseExceptionControllerAdvice {
    @ExceptionHandler
    @ResponseBody
    public Result exceptionHandler(InvalidAuthException e) {
        return new Result(ResponseCode.invalid_auth.getCode(), ResponseCode.invalid_auth.getMsg());
    }

    @ExceptionHandler
    @ResponseBody
    public Result exceptionHandler(VerifyException e) {
        return new Result(ResponseCode.param_validate_error.getCode(), e.getMessage());
    }

    @ExceptionHandler
    @ResponseBody
    public Result exceptionHandler(ResultException e) {
        return new Result(e.getCode(), e.getMsg());
    }

    @ExceptionHandler
    @ResponseBody
    public Result exceptionHandler(Exception e) {
        log.error("Server Error", e);
        return new Result(ResponseCode.server_error.getCode(), ResponseCode.server_error.getMsg());
    }
}
